﻿Ext.define('app.prm.project_task.EditTask',
{
    extend: 'Ext.window.Window',
    //autoScroll: true,
    modal: true,
    width: 950,
    height: 600,
    layout: 'fit',
    projectId: 0,
    projectTaskId: 0,
    bodyStyle: 'padding:3px',
    initComponent: function () {
        var me = this;
        me.items = [
            {
                xtype: 'panel',
                autoScroll: false,
                layout: 'border',
                border: false,
                items: [
                    {
                        xtype: 'panel',
                        region: 'center',
                        autoScroll: true,
                        bodyStyle: 'padding:5px',
                        items: [me.getTaskForm()]
                    },
                    {
                        xtype: 'panel',
                        region: 'east',
                        width: 300,
                        bodyStyle: 'padding:5px',
                        items: [
                            me.getSubTask()
                        ]
                    }
                ]
            }
        ];
        me.buttons = [
            { text: '保存', scope: this, handler: me.onSubmit },
            { text: '关闭', handler: function () { me.close(); } }
        ]
        me.callParent();
    },
    listeners: {
        afterrender: function (me) {
            if (me.projectTaskId > 0) {
                App.AjaxGet('/api/services/app/projectTask/Get?projectTaskId=' + me.projectTaskId, function (data) {
                    var formatDate = Ext.util.Format.date(data.result.planFinshTime, 'Y-m-d');
                    data.result["planFinshTime"] = formatDate;
                    me.getTaskForm().getForm().setValues(data.result);
                });
                me.getSubTask().store.load();
            }
        }
    },
    getSubTask: function () {
        var me = this;
        if (!this.subTask) {
            this.subTask = new Ext.grid.Panel({
                viewConfig: {
                    enableTextSelection: true //可以复制单元格文字
                },
                plugins: [
                    Ext.create('Ext.grid.plugin.CellEditing', { clicksToEdit: 1 })
                ],
                tbar: [
                    "->",
                    {
                        text: '新增',
                        glyph: 0xf067,
                        handler: function () {
                            me.subTask.getStore().insert(0, { subTitle: '', completed: '' });
                        }
                    },
                    {
                        text: '移除',
                        glyph: 0xf00d,
                        handler: function () {
                            var sels = me.subTask.getSelectionModel().getSelection();
                            if (sels.length == 0) {
                                return false;
                            }
                            me.subTask.store.remove(sels[0]);
                        }
                    }
                ],
                store: Ext.create("app.ux.PrmStore", { url: '/api/services/app/projectTask/GetSubTasks?taskId=' + me.projectTaskId, autoLoad: false }),
                title: '子任务',
                forceFit: true,
                columns: [
                    { header: '名称', width: 200, dataIndex: 'subTitle', editor: {} },
                    { xtype: 'checkcolumn', header: '是否已完成', width: 90, dataIndex: 'completed' }
                ]
            });
        }
        return this.subTask;
    },
    getRadioGroup: function () {
        var me = this;
        if (!this.radioGroup) {
            this.radioGroup = new Ext.form.RadioGroup({
                fieldLabel: '可见范围',
                items: [
                    { boxLabel: '项目内共享', name: 'range', checked: true, inputValue: 10 },
                    { boxLabel: '指定成员', name: 'range', inputValue: 20 }
                ],
                listeners: {
                    change: function (radio, newValue, oldValue, eOpts) {
                        if (newValue.range == 20) {
                            me.getTaskMember().show();
                            //me.getTaskMember().store.load();
                        } else {
                            me.getTaskMember().hide();
                        }
                    }
                }
            });
        }
        return this.radioGroup;
    },
    getTaskMember: function () {
        var me = this;
        if (!this.taskMember) {
            this.taskMember = new Ext.tree.Panel({
                border: true,
                hidden: true,
                style: 'margin:0 0 0 100px',
                useArrows: false,
                rootVisible: false,
                store: new Ext.data.TreeStore({
                    root: { expanded: true },
                    autoLoad: false,
                    nodeParam: 'parentId',
                    // 根节点的参数值是0
                    defaultRootId: "0",
                    proxy: {
                        type: 'ajax',
                        reader: {
                            type: 'json',
                            rootProperty: function (node) {
                                return node.result || node.children;
                            }
                        },
                        url: '/api/services/app/projectTask/GetRoleUserTree?projectId=' + me.projectId + "&taskId=" + me.projectTaskId
                    }
                }),
                root: { id: '0', text: '' },
                listeners: {
                    afterrender: function (tree) {
                        tree.expandAll();
                    },
                    checkchange: function (node, checked, eOpts) {
                        if (checked) {
                            node.eachChild(function (child) {
                                me.checkChild(child, true);
                            });
                        } else {
                            node.eachChild(function (child) {
                                me.checkChild(child, false);
                            });
                        }
                        me.checkParent(node);// 进行父级选中操作
                    }
                }
            });
        }
        return this.taskMember;
    },
    checkChild: function (node, check) {
        var me = this;
        node.set('checked', check);
        if (node.isNode) {
            node.eachChild(function (child) {
                me.checkChild(child, check);
            });
        }
    },
    nodep: function (node) {
        var bnode = false;
        Ext.Array.each(node.childNodes, function (v) {
            if (v.data.checked) {
                bnode = true;
                return;
            }
        });
        return bnode;
    },
    checkParent: function (node) {
        var me = this;
        if (node.parentNode != null) {
            if (me.nodep(node.parentNode)) {
                node.parentNode.set('checked', true);
            } else {
                node.parentNode.set('checked', false);
            }
            me.checkParent(node.parentNode);
        }
    },
    getTaskForm: function () {
        var me = this;
        if (!this.form) {
            this.form = new Ext.form.Panel({
                defaultType: 'textfield',
                title: '任务详细',
                border: true,
                defaults: { anchor: '95%' },
                fieldDefaults: {
                    labelWidth: 90,
                    labelAlign: "right",
                    flex: 1,
                    margin: 6
                },
                items: [
                    {
                        hidden: true,
                        name: 'id',
                        value: me.projectTaskId
                    },
                    {
                        hidden: true,
                        name: 'projectId',
                        value: me.projectId
                    },
                    {
                        xtype: 'container',
                        anchor: '95%',
                        layout: 'hbox',
                        items: [
                            {
                                xtype: 'container',
                                flex: 1,
                                layout: 'anchor',
                                items: [
                                    {
                                        xtype: 'textfield',
                                        fieldLabel: '工艺路线',
                                        name: 'route'
                                    },
                                    {
                                        xtype: 'textfield',
                                        fieldLabel: '所属',
                                        name: 'dept'
                                    },
                                    {
                                        xtype: 'numberfield',
                                        fieldLabel: '优先级',
                                        name: 'priority',
                                        allowDecimals: false,
                                        value: 0
                                    }
                                ]
                            },
                            {
                                xtype: 'container',
                                flex: 1,
                                layout: 'anchor',
                                items: [
                                    {
                                        xtype: 'textfield',
                                        fieldLabel: '步骤',
                                        name: 'step'
                                    },
                                    {
                                        xtype: 'datefield',
                                        format: 'Y-m-d',
                                        value: new Date(),
                                        allowBlank: false,
                                        fieldLabel: '计划完成时间',
                                        name: 'planFinshTime'
                                    }
                                ]
                            }
                        ]
                    },
                    me.getRadioGroup(),
                    me.getTaskMember(),
                    {
                        xtype: 'textarea',
                        height: 50,
                        emptyText: '逗号分割',
                        fieldLabel: '标签',
                        name: 'tags'
                    },
                    {
                        xtype: 'panel',
                        style: 'margin:0 0 0 100px',
                        border: false,
                        html: '<a href="#">选择标签</a>',
                        listeners: {
                            afterrender: function (component) {
                                component.getEl().on('click', function () {
                                    var win = Ext.create("app.prm.project_task.ChooseTag", { projectId: me.projectId });
                                    win.on("savesuccess", function (datas) {
                                        var tags = me.getTaskForm().query("textarea[name=tags]")[0];
                                        tags.setValue(tags.getValue() + "," + datas);
                                        win.close();
                                    });
                                    win.show();
                                });
                            }
                        }
                    },
                    {
                        xtype: 'textarea',
                        height: 200,
                        allowBlank: false,
                        fieldLabel: '问题',
                        name: 'title'
                    }
                ]
            });
        }
        return this.form;
    },
    onSubmit: function () {
        var me = this;

        var members = me.getTaskMember().getChecked();
        var values = [];
        for (var i = 0; i < members.length; i++) {
            var node = members[i];
            var userId = parseInt(node.data.id);
            if (userId > 0) {
                values.push(userId);
            }
        }

        var store = me.getSubTask().store;
        var subTasks = [];
        for (var i = 0; i < store.getCount() ; i++) {
            var record = store.getAt(i);
            subTasks.push(record.data);
        }

        if (!me.getTaskForm().getForm().isValid()) {
            return false;
        }

        var radio = me.getRadioGroup().getValue();
        if (radio.range == 20 && values.length == 0) {
            Ext.Msg.alert('系统提示', "请选择成员");
            return false;
        }

        var json = me.getTaskForm().getForm().getValues();
        json.users = values;
        json.subTasks = subTasks;
        App.AjaxPost('/api/services/app/projectTask/EditTask', json, function () {
            me.fireEvent("savesuccess");
        });
    }
});